﻿using Newtonsoft.Json;
using PHMEE.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PHMEE.MqttClient.MqttBusinessCommand
{
    /// <summary>
    /// 传输指标数据
    /// </summary>
    public class IndexDataCommand : BaseCommand
    {
        public override async Task ExecuteAsync(string requestInfo)
        {
            await Task.Run(() =>
            {
                List<IndexDataDto> data = null;
                try
                {
                    data = JsonConvert.DeserializeObject<List<IndexDataDto>>(requestInfo)!;
                    for (int i = 0; i < data.Count; i++)
                    {
                        Console.WriteLine($"通道号：{data[i].ChNo}");
                        Console.WriteLine($"通道类型：{data[i].ChType}");

                        Console.Write("时间：");
                        foreach (DateTime time in data[i].Time)
                        {
                            Console.Write($"{time.ToString("yyyy-MM-dd HH:mm:ss")}, ");
                        }
                        Console.WriteLine();
                        for (int j = 0; j < data[i].Content.Count; j++)
                        {
                            Console.WriteLine($"方项：{data[i].Content[j].Dir}");
                            Console.WriteLine($"指标类型：{data[i].Content[j].Code}");

                            Console.Write("数值：");
                            foreach (float value in data[i].Content[j].Values)
                            {
                                Console.Write($"{value}, ");
                            }
                            Console.WriteLine();
                        }
                        Console.WriteLine("\n");
                    }
                }
                catch (Exception e)
                {
                    Status = -3;
                    return;
                }

                string fileName = $@"IndexData_{DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss")}.txt";
                //WriteFileHelper.WriteFile(SN, fileName, requestInfo);
                //isReplay = true;
            });
        }
    }
}
